﻿using System.Threading;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.Common;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using MbUnit.Framework;

////********************************************************************************************************************************************
////Created by: Claudia De Leon
////Date created: 06/04/2013
////ULTI-103917 Location Business Rules: Disable Local Taxes (US) if employee is attached to location in .NET https://ultidev/browse/ULTI-103917
////********************************************************************************************************************************************

/*              Data set up for test cases below:
 *  ||=======||=================================================||
 *  || CODE  || DESCRIPTION                                     || 
 *  ||=======||=================================================||
 *  || CDNEE || Location with no employees and no salary diff   ||
 *  ||=======||=================================================||
 *  ||000001 || Location with employees                         || 
 *  ||=======||=================================================||
 *  ||JSDNEE || Location with salary differential               ||
 *  ||=======||=================================================||
 *  || PRPA  || Location with employees and salary differencial ||
 *  ||=======||=================================================||
 */

namespace Echo.Automation.Compliance.BusinessRules.Locations
{
    [TestFixture]
    [Author("Claudia De Leon", "claudia_deleon@ultimatesoftware.com")]
    [Category("Business Rules")]
    [Header("Config")]
    //[Config(".NET")]
    [Config(".NET")]
    public class DisableLocalTaxesUS : MbUnitAutomatedTest
    {
        #region Setups / Teardowns
        UltiPro UltiPro;
        UltiProDatabaseContext Database;
        
        [FixtureSetUp]
        public void FixtureSetUp()
        {
            #region Setup database ULTIPRO_CALENDAR
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");
            Database.Company.AddRollBack("iLocation", string.Format("code = '{0}'", "CDNEE"));
            Database.Company.AddRollBack("iLocation", string.Format("code = '{0}'", "000001"));
            Database.Company.AddRollBack("iLocation", string.Format("code = '{0}'", "JSDNEE"));
            Database.Company.AddRollBack("iLocation", string.Format("code = '{0}'", "PRPA"));
            Database.Company.AddRollBack("Loccomp", string.Format("lcpCode = '{0}'", "CDNEE"));
            Database.Company.AddRollBack("Loccomp", string.Format("lcpCode = '{0}'", "000001"));
            Database.Company.AddRollBack("Loccomp", string.Format("lcpCode = '{0}'", "JSDNEE"));
            Database.Company.AddRollBack("Loccomp", string.Format("lcpCode = '{0}'", "PRPA"));
            Database.Company.AddRollBack("EmpMLoc", string.Format("emlCode = '{0}'", "CDNEE"));
            #endregion

            #region Login as Admin USA-CANU 
            UltiPro.Pages.Login.LoginAs("usa-canu", "password", Config.LanguageCode);
            #endregion
        }
        
        [FixtureTearDown]
        public void FixtureTearDown()
        {
            UltiPro.Browser.Quit();
            Database.Company.RollBackDatabase();
        }
        #endregion
        
        [Test]
        [Row("000001", "000001 Automation LocView")]
        [Row("PRPA", "PR Penn")]
        public void LocalTaxesFieldsDisabled(string loccode, string locdescr)
        {
            #region GIVEN that I am on the Edit page for a Location that is tied to employees
            //Assert location is tied to employees
            int count;
            int.TryParse(Database.Company.GetValue("SELECT COUNT(location.locCode) FROM location JOIN empmloc ON location.loccode = empmloc.emlcode WHERE location.locaddresscountry = 'USA' AND location.loccode = '" + loccode + "'"), out count);
            Assert.GreaterThanOrEqualTo(count, 1);
            UltiPro.Macros.Navigate.ToLocationBusinessRule(loccode);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            UltiPro.Pages.AddChgLocations.Is(header => header.PageHeaderLabel, "innerHTML", "Add/Change Location");
            UltiPro.Pages.AddChgLocations.Is(value => value.LocationCountryValue, "innerHTML", "United States");
            UltiPro.Pages.AddChgLocations.Is(value => value.CodeValue, "innerHTML", loccode);
            UltiPro.Pages.AddChgLocations.Is(value => value.DescriptionTextbox.EnglishTextBox, "value", locdescr);
            #endregion
            
            #region WHEN the page is on Edit mode
            UltiPro.Pages.AddChgLocations.ButtonBar.Exists(button => button.Save);
            UltiPro.Pages.AddChgLocations.ButtonBar.Exists(button => button.Reset);
            UltiPro.Pages.AddChgLocations.ButtonBar.Exists(button => button.Cancel);
            #endregion
            
            #region THEN the Local Taxes fields are disabled
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkInCountyDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkInTaxResidentRateDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkInTaxNonResidentRateDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.OccupationalDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkersCompensationDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.OtherDropdown);
            #endregion

            #region WHEN I click the reset button
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Reset);
            #endregion
            
            #region THEN the fields remain disabled
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkInCountyDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkInTaxResidentRateDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkInTaxNonResidentRateDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.OccupationalDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkersCompensationDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.OtherDropdown);
            #endregion

            #region WHEN I click on the View Links and rates button
            UltiPro.Pages.AddChgLocations.Click(button => button.LinksAndRatesButton);
            #endregion
            
            #region Then the radio buttons for all the sections are disabled
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.NotEnabled(radioButton => radioButton.OccupationalTaxCurrentRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.NotEnabled(radioButton => radioButton.OccupationalTaxRecommendedRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.NotEnabled(radioButton => radioButton.WorkersCompTaxCurrentRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.NotEnabled(radioButton => radioButton.OccupationalTaxRecommendedRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.NotEnabled(radioButton => radioButton.OtherTaxCurrentRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.NotEnabled(radioButton => radioButton.OtherTaxRecommendedRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Click(button => button.CancelButton);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
        }

        [Test]
        [Row("CDNEE", "CD No EE or Sal. Diff.")]
        [Row("JSDNEE", "Jannette's Sal Diff No EE")]
        public void LocalTaxesFieldsEnabled(string loccode, string locdescr)
        {
            #region GIVEN that I am on the Edit page for a Location that is NOT tied to employees
            //Assert location is not tied to employees
            int count;
            int.TryParse(Database.Company.GetValue("SELECT COUNT(location.locCode) FROM location JOIN empmloc ON location.loccode = empmloc.emlcode WHERE location.locaddresscountry = 'USA' AND location.loccode = '" + loccode + "'"), out count);
            Assert.AreEqual(count, 0);
            UltiPro.Macros.Navigate.ToLocationBusinessRule(loccode);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            UltiPro.Pages.AddChgLocations.Is(header => header.PageHeaderLabel, "innerHTML", "Add/Change Location");
            UltiPro.Pages.AddChgLocations.Is(value => value.LocationCountryValue, "innerHTML", "United States");
            UltiPro.Pages.AddChgLocations.Is(value => value.CodeValue, "innerHTML", loccode);
            UltiPro.Pages.AddChgLocations.Is(value => value.DescriptionTextbox.EnglishTextBox, "value", locdescr);
            #endregion
            
            #region WHEN the page is on Edit mode
            UltiPro.Pages.AddChgLocations.ButtonBar.Exists(button => button.Save);
            UltiPro.Pages.AddChgLocations.ButtonBar.Exists(button => button.Reset);
            UltiPro.Pages.AddChgLocations.ButtonBar.Exists(button => button.Cancel);
            #endregion
            
            #region THEN the Local Taxes fields are enabled
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInCountyDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInTaxResidentRateDropdown);
            //UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInTaxNonResidentRateDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.OccupationalDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkersCompensationDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.OtherDropdown);
            #endregion

            #region WHEN I click the reset button
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Reset);
            #endregion
            
            #region THEN the fields remain enabled
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInCountyDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInTaxResidentRateDropdown);
            //UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInTaxNonResidentRateDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.OccupationalDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkersCompensationDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.OtherDropdown);
            #endregion

            #region WHEN I click on the View Links and rates button
            UltiPro.Pages.AddChgLocations.Click(button => button.LinksAndRatesButton);
            #endregion
            
            #region Then the radio buttons for all the sections are enabled
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.OccupationalTaxCurrentRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.OccupationalTaxRecommendedRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.WorkersCompTaxCurrentRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.OccupationalTaxRecommendedRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.OtherTaxCurrentRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.OtherTaxRecommendedRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Click(button => button.CancelButton);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
        }

        [Test]
        public void CreateNewLocation()
        {
            #region GIVEN I create a US Location
            UltiPro.Macros.Navigate.ToBusinessRule("Locations");
            UltiPro.Pages.LocationSummary.ButtonBar.Click(button => button.Add);
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.LocationCountryDropdown, "USA", SelectOption.Value);
            #endregion
            
            #region WHEN I set the Wok-in-state tax to ORSIT
            Thread.Sleep(5000);
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.WorkInStateDropdown, "ORSIT", SelectOption.Value);
            #endregion
            
            #region THEN the local taxes section is enabled
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInCountyDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInTaxResidentRateDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInTaxNonResidentRateDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.OccupationalDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkersCompensationDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.OtherDropdown);
            #endregion
            
            #region And the radio buttons on the View Links and rates window for all the sections are enabled
            UltiPro.Pages.AddChgLocations.Click(button => button.LinksAndRatesButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.OccupationalTaxCurrentRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.OccupationalTaxRecommendedRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.WorkersCompTaxCurrentRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.OccupationalTaxRecommendedRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.OtherTaxCurrentRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Enabled(radioButton => radioButton.OtherTaxRecommendedRadioButton);
            UltiPro.Pages.AddChgLocations.LocationViewLinksAndRatesDialog.Click(button => button.CancelButton);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
        }

        // These tests change values on fields used on previous test. Order added to avoid Rollback on Teardown
        [Test(Order = 1)] 
        [Row("CDNEE", "CD No EE or Sal. Diff.", "ADAMS - ADAMS", "PA100252 - MT PLEASANT - CONEWAGO VALLEY", "", "PA112931 - GETTYSBURG BORO - $47.00", "", "PA113906 - UPPER ADAMS SD - $10.00")]
        [Row("000001", "000001 Automation LocView", "ALLEGHENY - ALLEGHENY", "PA111492 - AVALON", "", "PA111494 - AVALON BORO - $47.00", "PAPITTER - Pittsburgh Payroll Exp ER - 0.55%", "PA111472 - ALLEGHENY VALLEY - $5.00")]
        public void EditOtherFieldsOnPage(string loccode, string locdescr, string workInCounty, string workInResRate, string workInNonResRate, string occp, string workersCmp, string other)
        {
            #region GIVEN I open a location with local taxes
            UltiPro.Macros.Navigate.ToLocationBusinessRule(loccode);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            UltiPro.Pages.AddChgLocations.Is(header => header.PageHeaderLabel, "innerHTML", "Add/Change Location");
            UltiPro.Pages.AddChgLocations.Is(value => value.CodeValue, "innerHTML", loccode);
            UltiPro.Pages.AddChgLocations.Is(value => value.DescriptionTextbox.EnglishTextBox, "value", locdescr);
            #endregion
            
            #region WHEN I edit other page fields such as Details, Address and Company
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.DescriptionTextbox.EnglishTextBox, "New description");
            UltiPro.Pages.AddChgLocations.Click(checkbox => checkbox.InactiveCheckbox);
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.ReportingCategoryDropdown, "ALL Rep Cat", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.PhoneNumberTextbox, "7862583382");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Save);
            #endregion
            
            #region THEN the Local taxes fields retain their values
            UltiPro.Pages.AddChgLocations.Is(value => value.WorkInCountyValue, "innerHTML", workInCounty);
            UltiPro.Pages.AddChgLocations.Is(value => value.WorkInTaxResidentRateValue, "innerHTML", workInResRate);
            UltiPro.Pages.AddChgLocations.Is(value => value.WorkInTaxNonResidentRateValue, "innerHTML", workInNonResRate);
            UltiPro.Pages.AddChgLocations.Is(value => value.OccupationalValue, "innerHTML", occp);
            UltiPro.Pages.AddChgLocations.Is(value => value.WorkersCompensationValue, "innerHTML", workersCmp);
            UltiPro.Pages.AddChgLocations.Is(value => value.OtherValue, "innerHTML", other);
            #endregion
        }

        [Test(Order = 2)] 
        [Row("CDNEE")]
        public void AssignEmployeesToLocation(string loccode)
        {
            #region GIVEN there is a Location with no employees assigned and local taxes enabled
            //Assert location is not tied to employees
            int count;
            int.TryParse(Database.Company.GetValue("SELECT COUNT(location.locCode) FROM location JOIN empmloc ON location.loccode = empmloc.emlcode WHERE location.locaddresscountry = 'USA' AND location.loccode = '" + loccode + "'"), out count);
            Assert.AreEqual(count, 0);
            UltiPro.Macros.Navigate.ToLocationBusinessRule(loccode);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            UltiPro.Pages.AddChgLocations.Is(value => value.CodeValue, "innerHTML", loccode);
            // Assert Local taxes dropdowns are enabled
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInCountyDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInTaxResidentRateDropdown);
            //UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkInTaxNonResidentRateDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.OccupationalDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.WorkersCompensationDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.OtherDropdown);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            #endregion
            
            #region WHEN I assign the location to an employee
            UltiPro.Macros.Navigate.ToEmployee("555667788");
            UltiPro.Menu.MyTeam.SubMenu.Click(tab => tab.Taxes);
            UltiPro.Menu.MyTeam.SubMenu.Taxes.Click(tab => tab.WorkLocation);
            UltiPro.Pages.EeAdminTaxLocationSetup.ButtonBar.Click(button => button.Add);
            UltiPro.Pages.EeAdminTaxChgLocationState.Set(dropdown => dropdown.WorkLocationDropdown, loccode, SelectOption.Value);
            UltiPro.Pages.EeAdminTaxChgLocationState.Set(dropdown => dropdown.FilingStatusDropDown, "M", SelectOption.Value);
            UltiPro.Pages.EeAdminTaxChgLocationState.ButtonBar.Click(button => button.Save);
            UltiPro.Browser.Close();
            UltiPro.Browser.SwitchToMainWindow();
            #endregion
            
            #region THEN the local taxes section is disabled
            UltiPro.Macros.Navigate.ToLocationBusinessRule(loccode);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkInCountyDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkInTaxResidentRateDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkInTaxNonResidentRateDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.OccupationalDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.WorkersCompensationDropdown);
            UltiPro.Pages.AddChgLocations.NotEnabled(dropdown => dropdown.OtherDropdown);
            #endregion
        }
    }
}
